package com.yicoollxing.controller.backend;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yicoollxing.common.Const;
import com.yicoollxing.common.ServerResponse;
import com.yicoollxing.pojo.User;
import com.yicoollxing.pojo.Voucher;
import com.yicoollxing.service.IUserService;
import com.yicoollxing.service.IVoucherService;


@Controller
@RequestMapping("/manager/voucher")
public class VoucherManagerController {
   //增删改查
	
	@Autowired
	private IUserService iUserService;
	
	@Autowired
	private IVoucherService iVoucherService;
	
	
	/**
	 * 修改，添加优惠券
	 * @param voucher
	 * @param httpSession
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add_modify.do")
	public ServerResponse modifyAndAdd(Voucher voucher,HttpSession httpSession){
		User user=(User) httpSession.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=iUserService.checkAdminRole(user);
		if(!response.isSuccess()){
			return response;
		}
		
		return iVoucherService.modifyAndAdd(voucher);
	}
	
	
	/**
	 * 删除优惠券
	 * @param voucherId
	 * @param httpSession
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/del.do")
	public ServerResponse  delVoucher(Integer voucherId,HttpSession httpSession){
		User user=(User) httpSession.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=iUserService.checkAdminRole(user);
		if(!response.isSuccess()){
			return response;
		}
		return iVoucherService.delVoucher(voucherId);
	}
	
	
	/**
	 * 查询优惠券
	 * @param voucherId
	 * @param httpSession
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/search.do")
	public ServerResponse search(Integer voucherId,Integer useClassId,String sourceName,
			 @RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
			 @RequestParam(value="pageSize",defaultValue="10")Integer pageSize,HttpSession httpSession){
		User user=(User) httpSession.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=iUserService.checkAdminRole(user);
		if(!response.isSuccess()){
			return response;
		}
		return iVoucherService.search(voucherId, useClassId, sourceName, pageNum, pageSize);
	}
}
